* Author: Joe Tatarka
* Name: 3_3_gws_build.do
* Purpose: Create a build from the GWS data 
* Combine the GWS Panel and GWS App Delivery to create a final dataset 

* Set Global File Paths
global root = "T:/service_industries/replication_package"

global raw_root = "${root}/datasets/raw"
global intermediate_root = "${root}/datasets/intermediate"
global built_root = "${root}/datasets/built"
global exhibits_root = "${root}/exhibits"

global gws_root = "T:/generally_useful_datasets/gws_06_17_24_pull/clean"

******************************
**** Step 1: Bring in the panel 
use "${intermediate_root}/3_2_gws_app_usage.dta", clear

***********************************************************************
******* Step 2: Merge App Usage with how many days the panelist days there are 
sort year_month_gs
merge 1:1 year_month_gs using "${intermediate_root}/3_1_gws_panel.dta", nogen keep(1 3)

**********************************************************
**** Step 3: Create App Usage per day for delivery and driver apps
** Duration per number of days people are in the panel in that given month
gen delivery_use_per_day = app_use_all_delivery/panel_days
gen driver_use_per_day = app_use_all_driver/panel_days

*** Index to 2019 Average values 
foreach var of varlist delivery_use_per_day driver_use_per_day {
	egen `var'_2019 = mean(`var') if year_month_gs <= monthly("dec2019", "MY") & year_month_gs >= monthly("jan2019", "MY")
	egen m`var'_2019 = max(`var'_2019)
	gen `var'_idx2019 = 100*(`var'/m`var'_2019)
	drop `var'_2019 m`var'_2019
}

label var delivery_use_per_day "Delivery App Usage (Aggregate Minutes per Day)"
label var delivery_use_per_day "Driver App Usage (Aggregate Minutes per Day)"
label var delivery_use_per_day_idx2019 "Delivery App Usage (2019 Avg = 100)"
label var delivery_use_per_day_idx2019 "Driver App Usage (2019 Avg = 100)"

**** Keep from Jan 2019 - Dec 2023
keep if year_month_gs >= monthly("jan2019", "MY") & year_month_gs <= monthly("dec2023", "MY")

********************************************************************************
**** Save Dataset
********************************************************************************
save "${built_root}/gws_build.dta", replace


